package com.gugee.tiktok.data.consumer.blogger.core.main;

import com.gugee.tiktok.common.model.dal.market.BloggerMarketRepository;
import com.gugee.tiktok.common.model.mysql.BloggerMarket;
import com.gugee.tiktok.common.model.spider.market.author.Author;
import com.gugee.tiktok.common.model.spider.market.distribution.Property;
import com.gugee.tiktok.data.common.msg.MarketMessage;
import com.gugee.tiktok.data.consumer.blogger.config.DataConsumerConfig;
import com.gugee.tiktok.data.consumer.blogger.core.spider.MarketSpiderClient;
import com.xiguaji.boot.toolkit.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: luolifeng
 * Date: 2020-11-16 15:06
 */
@Service
@Slf4j
public class BloggerMarketDistributionMain {
    @Autowired
    MarketSpiderClient marketSpiderClient;
    @Autowired
    BloggerMarketRepository bloggerMarketRepository;
    @Value("${xiguaji.spider.host}")
    String host;
    @Autowired
    @Qualifier("kafkaTemplateMarket")
    private KafkaTemplate<Long, MarketMessage> kafkaTemplateMarket;

    @Autowired
    DataConsumerConfig dataConsumerConfig;

    public void handle(MarketMessage marketMessage){
            try {
                run(marketMessage.getAuthor(), "MarketForkJoinTask");
            } catch (Exception e) {
                log.error("error={},uid={}", e.getMessage(), marketMessage.getAuthor().getCore_user_id());
                if(marketMessage.getRetryTimes()>0){
                    marketMessage.setRetryTimes(marketMessage.getRetryTimes()-1);
                    kafkaTemplateMarket.send(dataConsumerConfig.getMarketOnlineTopic(),marketMessage);
                }
            }

    }

    /** 爬取播主星图详情，并更新入库
     * 此部分为原代码搬迁
     * @param author 播主信息
     * @param eventSource 记录操作者
     */
    public void run(Author author, String eventSource){
        long start=System.currentTimeMillis();
        BloggerMarket bloggerMarket = bloggerMarketRepository.findByMarketId(author.getId());

        if(null == bloggerMarket){
            //bloggerMarket.setId(1);
            bloggerMarket = new BloggerMarket();
            bloggerMarket.setCoreUserId(author.getCore_user_id());
            bloggerMarket.setMarketId(author.getId());
            bloggerMarket.setCreateBy(eventSource);
            bloggerMarket.setUpdateBy(eventSource);
            bloggerMarket.setAgeDistribution("");
            bloggerMarket.setGenderDistribution("");
            bloggerMarket.setRegionDistribution("");
            bloggerMarket.setRegionGeonameidDistribution("");
            bloggerMarket.setStateDistribution("");
            bloggerMarket.setStateGeonameidDistribution("");
        }
        bloggerMarket.setId(bloggerMarket.getId());
        bloggerMarket.setECart(author.getE_cart() != 0);
        bloggerMarket.setAgeMaxRatio(author.getAudience_profile().getAge().getMax_ratio());
        bloggerMarket.setAgeMaxDistribution(author.getAudience_profile().getAge().getMax_distribution());
        bloggerMarket.setGenderMaxRatio(author.getAudience_profile().getGender().getMax_ratio());
        bloggerMarket.setGenderMaxDistribution(author.getAudience_profile().getGender().getMax_distribution());
        bloggerMarket.setRegionMaxRatio(author.getAudience_profile().getRegion().getMax_ratio());
        bloggerMarket.setRegionMaxDistribution(author.getAudience_profile().getRegion().getMax_distribution());

        val distribution = marketSpiderClient.getDistribution(bloggerMarket.getMarketId());
        for (Property property : distribution.getData().getActive()) {
            if(property.getProperty().equals("active")){
                bloggerMarket.setActive(property.getValue());
            }else if(property.getProperty().equals("inactive")){
                bloggerMarket.setInactive(property.getValue());
            }
        }
        bloggerMarket.setAgeDistribution(JsonUtil.parseJson(distribution.getData().getAge()));
        bloggerMarket.setGenderDistribution(JsonUtil.parseJson(distribution.getData().getGender()));
        bloggerMarket.setRegionDistribution(JsonUtil.parseJson(distribution.getData().getRegion()));
        bloggerMarket.setRegionGeonameidDistribution(JsonUtil.parseJson(distribution.getData().getRegion_geonameid()));
        bloggerMarket.setStateDistribution(JsonUtil.parseJson(distribution.getData().getState()));
        bloggerMarket.setStateGeonameidDistribution(JsonUtil.parseJson(distribution.getData().getState_geonameid()));
        bloggerMarket.setUpdateBy(eventSource);
        bloggerMarketRepository.save(bloggerMarket);
        log.info("用户更新成功:uid={},耗时={}", author.getCore_user_id(),(System.currentTimeMillis() - start) / 1000);
//        XxlJobLogger.log("用户更新成功:uid={},耗时={}", author.getCore_user_id(),(System.currentTimeMillis() - start) / 1000);

    }
}
